{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">数据库列表</div>
    <div class="layui-card-body">
        <table class="layui-hide" id="currentTable" lay-filter="currentTable" 
            data-auth-restore="{:$auth->check('database/restore')}"
            data-auth-export="{:$auth->check('database/export')}"
            data-auth-optimize="{:$auth->check('database/optimize')}"
            data-auth-repair="{:$auth->check('database/repair')}"
        ></table>
    </div>
</div>
{/block}
{block name="script"}
<script>
layui.use(['yznTable', 'form','table'], function() {
    var yznTable = layui.yznTable,
        table = layui.table,
        $ = layui.$,
        form = layui.form,
        layer = layui.layer;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTable',
    };

    yznTable.render({
        init: init,
        toolbar: ['refresh',[
            {
               html: "<a class='layui-btn layui-btn-sm layui-btn-danger' href='{:url('restore')}'><i class='iconfont icon-other'></i> 数据库还原</a>",
               auth: 'restore',
            },
             {
               html: '<a class="layui-btn layui-btn-sm" id="export"><i class="iconfont icon-beifenruanjian"></i> 立即备份</a>',
               auth: 'export',
            },
            {
               html: "<a class='layui-btn layui-btn-sm' id='optimize' href='{:url('optimize')}'><i class='iconfont icon-reactjs-fill'></i> 优化表</a>",
               auth: 'optimize',
            },
            {
               html: "<a class='layui-btn layui-btn-sm' id='repair' href='{:url('repair')}'><i class='iconfont icon-tools-line'></i> 修复表</a>",
               auth: 'repair',
            }
        ]],
        url: '{:url("index")}',
        cols: [
            [
                { type: 'checkbox', fixed: 'left' },
                { field: 'name', width: 200, title: '表名' },
                { field: 'rows', width: 120, title: '数据量' },
                { field: 'data_length', width: 120, title: '数据大小', templet: '#size' },
                { field: 'create_time', width: 200, title: '创建时间' },
                { field: 'comment', title: '说明' },
                { field: 'info', width: 200, title: '备份状态', templet: '<div>未备份</div>' },
            ]
        ],
    });

    yznTable.bindevent();

    var $export = $("#export");
        
    //优化or修复
    $(document).on('click', '#optimize,#repair', function() {
        var self = this,
            checkStatus = table.checkStatus('currentTable');
        if (checkStatus.data.length <= 0) {
            layer.msg("请选择需要操作的数据",{icon: 2});
            return false;
        }
        var a = [];
        $(checkStatus.data).each(function(i, o) {
            a.push(o.name);
        });
        $.post(self.href, { tables: a }, function(data) {
            if (data.code) {
                layer.msg(data.msg, { icon: 1 });
            } else {
                layer.msg(data.msg, { icon: 5 });
            }
        }, "json");
        return false;
    });

    //提交备份
    $(document).on('click', '#export', function() {
        var checkStatus = table.checkStatus('currentTable');
        if (checkStatus.data.length <= 0) {
            layer.msg("请选择需要备份的数据",{icon: 2});
            return false;
        }
        var a = [];
        $(checkStatus.data).each(function(i, o) {
            a.push(o.name);
        });
        $export.parent().children().addClass("layui-btn-disabled");
        $export.html("<i class='icon iconfont icon-fasong'></i>正在发送备份请求...");
        $.post("{:url('export')}", { tables: a },
            function(data) {
                if (data.code) {
                    tables = data.data.tables;
                    $export.html(data.msg + "开始备份，请不要关闭本页面！");
                    backup(data.data.tab);
                    window.onbeforeunload = function() { return "正在备份数据库，请不要关闭！" }
                } else {
                    layer.msg(data.msg, { icon: 5 });
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                }
            },
            "json"
        );
        return false;
    });

    function backup(tab, code) {
        code && showmsg(tab.id, "开始备份...(0%)");
        $.get("{:url('export')}", tab, function(data) {
            if (data.code) {
                showmsg(tab.id, data.msg);
                if (!$.isPlainObject(data.data.tab)) {
                    $export.parent().children().removeClass("layui-btn-disabled");
                    $export.html("备份完成，点击重新备份");
                    window.onbeforeunload = function() { return null }
                    return;
                }
                backup(data.data.tab, tab.id != data.data.tab.id);
            } else {
                layer.msg(data.msg, { icon: 5 });
                $export.parent().children().removeClass("disabled");
                $export.html("立即备份");
            }
        });
    }

    function showmsg(id, msg) {
        $('.layui-table').find('tr:eq('+id+') td:eq(6)').find('div').html(msg);
    }
});
</script>
{/block}